﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureInstanceTransformationHandler.h"
#include "ElementTransformationHandlerManager.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "GbmpBuiltInParameterDefinitions.h"
#include "IElement.h"
#include "IElementBasicInformation.h"
#include "IElementParameters.h"
#include "IElementTransformationComponent.h"
#include "IModelView.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"
#include "Vector3dUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

REGISTER_ELEMENT_TRANSFORMATION_CUSTOMIZER(StructureFloorTransformationHandler);

bool StructureFloorTransformationHandler::Match(const IElement* pElement) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"pElement为空",L"GDMPLab",L"2024-03-30");

    return pElement->GetBasicInformation()->GetCategoryUid() == BuiltInCategoryUniIdentities::BICU_STRUCTURE_FLOOR;
}

bool StructureFloorTransformationHandler::Translate(IElement* pMoveElement, const ElementTransformationContext& transformContext)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pMoveElement, L"pMoveElement为空",L"GDMPLab",L"2024-03-30");

    const IModelView* pModelView = transformContext.ModelView;
    if (pModelView && Vector3dUtils::IsPerpendicular(pModelView->GetViewDirection(), Vector3d::UnitZ))
    {
        // Z向的拖拽转化为修改顶部偏移值
        double zDelta = transformContext.MoveContext.GetMoveVector().Z();
        IElementParameters* pParamBehavior = pMoveElement->GetElementParameters();
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pParamBehavior, L"pParamBehavior为空",L"GDMPLab",L"2024-03-30");
        OwnerPtr<IParameter> paramTopOffset = pParamBehavior->GetParameterByUid(PARAMETER_UID(TopOffsetBuiltInParameter));
        DBG_WARN_AND_RETURN_FALSE_UNLESS(IParameter::IsValidParameter(paramTopOffset), L"paramTopOffset无效",L"GDMPLab",L"2024-03-30");
        double newTopOffset = paramTopOffset->GetValueAsDouble() + zDelta;
        paramTopOffset->SetValueAsDouble(newTopOffset);
        std::wstring errorMsg;
        bool bOK = pParamBehavior->SetParameter(paramTopOffset.get(), &errorMsg);
        DBG_WARN_AND_RETURN_FALSE_UNLESS(bOK, L"SetParameter失败",L"GDMPLab",L"2024-03-30");

        // 其他方向的拖拽由PositionHelper处理
        return ElementTransformationHandler::Translate(pMoveElement, transformContext);
    }

    return ElementTransformationHandler::Translate(pMoveElement, transformContext);
}

//////////////////////////////////////////////////////////////////////////
